package cc.telecomdigital.tdstock.activity.groups.more;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.Http.H_CODE;
import cc.telecomdigital.tdstock.Http.HttpSuperClassStockBaseActivity;
import cc.telecomdigital.tdstock.Http.Services.HttpAppInfo;
import cc.telecomdigital.tdstock.Http.Services.H_HttpServiceRequest.H_UserClient_HttpRequest_Result;
import cc.telecomdigital.tdstock.Tools.ActivityGroupController;
import cc.telecomdigital.tdstock.activity.groups.MoreGroup;

/**
 * 財星List
 * 
 * @author ouyangzhihui
 */
public class StockStarListActivity extends HttpSuperClassStockBaseActivity implements OnClickListener, OnItemClickListener
{
	final static private String	TAG			= "StockStarListActivity";

	static public String		iTypeName	= "";
	static public boolean 		iContentPageBack = false;
	private String				lastTimestame = null;

	private List<String>		_list		= new ArrayList<String>();
	private ListItemAdapter		_adapter;
	private ListView			_listview;

	private TextView			_top_bar_title;
	
	static private int[]		starArray = 
	{
		R.string.more_stockstar_01,
		R.string.more_stockstar_02,
		R.string.more_stockstar_03,
		R.string.more_stockstar_04,
		R.string.more_stockstar_05,
		R.string.more_stockstar_06
	};

	@Override
	protected ActivityGroupController GetActivityGroupController()
	{
		return MoreGroup.GetActivityGroupHostControl();
	}

	private class ListItemAdapter extends BaseAdapter
	{
		private LayoutInflater	iInflater;
		private List<String>	data;

		public ListItemAdapter(Context iContext, List<String> list)
		{
			iInflater = LayoutInflater.from(iContext);
			data = list;
		}

		private class ViewHolder
		{
			public TextView		seq;
			public TextView		txt;
			public ImageView	sign;
		}

		@Override
		public int getCount()
		{
			return data.size();
		}

		@Override
		public Object getItem(int position)
		{
			return data.get(position);
		}

		@Override
		public long getItemId(int position)
		{
			long itemId = position;
			try
			{
				String tmp = data.get(position); // -1: not data, -2: paging.
				try
				{
					itemId = Long.parseLong(tmp);
				}
				catch (Exception e)
				{
				}
			}
			catch(Exception e)
			{
			}
			return itemId;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			ViewHolder holder = null;
			if (convertView == null)
			{
				convertView = iInflater.inflate(R.layout.news_list_item, null);
				holder = new ViewHolder();
				holder.seq = (TextView) convertView.findViewById(R.id.news_seq);
				holder.txt = (TextView) convertView.findViewById(R.id.news_title);
				holder.sign = (ImageView) convertView.findViewById(R.id.news_nextpage);
				
				convertView.setDrawingCacheBackgroundColor(Color.WHITE);

				convertView.setTag(holder);
			}
			else
			{
				holder = (ViewHolder) convertView.getTag();
			}

			long id = getItemId(position);
			if (id == -1)
			{
				holder.seq.setText("");
				holder.txt.setText(R.string.provide_no_information);
				holder.txt.setTextColor(0xff5597f9);
				holder.sign.setImageDrawable(null);
				holder.sign.setBackgroundDrawable(null);
			}
			else if (id == -2)
			{
				holder.seq.setText("");
				holder.txt.setText(R.string.record_tips_next);
				holder.txt.setTextColor(0xff5597f9);
				holder.sign.setImageDrawable(null);
				holder.sign.setBackgroundDrawable(null);
			}
			else if (id == -3)
			{
				holder.seq.setText("");
				holder.txt.setText(R.string.record_tips_eof);
				holder.txt.setTextColor(Color.BLACK);
				holder.sign.setImageDrawable(null);
				holder.sign.setBackgroundDrawable(null);
			}
			else
			{
				holder.seq.setText(String.valueOf(position + 1));
				holder.txt.setText(data.get(position));
				holder.txt.setTextColor(Color.BLACK);
				holder.sign.setImageResource(R.drawable.go_next_icon);
			}
			return convertView;
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.more_stockstar_list);

		findViewById(R.id.more_returnbtn).setOnClickListener(this);

		_top_bar_title = (TextView) findViewById(R.id.top_bar_title);

		_adapter = new ListItemAdapter(this, _list);

		_listview = (ListView) findViewById(R.id.listview);
		_listview.setAdapter(_adapter);
		_listview.setOnItemClickListener(this);
	}

	@Override
	protected void onResume()
	{
		if(!iContentPageBack)
		{
			lastTimestame = null;
		}
		
		super.onResume();
		int index = Integer.parseInt(iTypeName) - 1;
		_top_bar_title.setText(starArray[index]);
		_listview.invalidateViews();
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
			case R.id.more_returnbtn:
				StockStarIndexActivity.iContentPageBack = true;
				SwitchForwardActivity(StockStarIndexActivity.class);
				break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id)
	{
		if (id == -2) // 继续下载资料
		{
			SendHttpRequest();
		}
		else if (id >= 0) // 跳转页面
		{
			TDStockLog.d(TAG, "position:" + position);
			StockStarContentActivity.iPosition = (position + 1);
			StockStarContentActivity.iTypeName = iTypeName;
			StockStarContentActivity.iContent = _list.get(position);
			StockStarContentActivity.iFromClass = StockStarListActivity.class;
			StockStarContentActivity.contentList.clear();
			StockStarContentActivity.contentList.addAll(_list);
			SwitchForwardActivity(StockStarContentActivity.class);
		}
	}

	@Override
	protected H_CODE GetDataFromHttpServer()
	{
		if(iContentPageBack)
		{
			iContentPageBack = false;
			return null;
		}
		
		TDStockLog.d(TAG, "TagKey: " + iTypeName);
		
		if(lastTimestame == null)
		{
			_list.clear();
			_adapter.notifyDataSetChanged();
		}
		
		String[] iTime = HttpAppInfo.GetDataTimeParamsByYear(lastTimestame);
		
		TDStockLog.d(TAG, "Time:" + iTime[0] + "," + iTime[1]);
		
		return HttpAppInfo.SendStockStarRequest(actContext, iTypeName, iTime[0], iTime[1], this);
	}

	@Override
	protected void H_UserClient_HttpRequest_Response_Sub(H_UserClient_HttpRequest_Result iResult)
	{
		if (!iResult.MStatus.equals("0"))
		{
			ShowMessageDialog(iResult.MError + "(" + iResult.MStatus + ")");
			return;
		}
		TDStockLog.d(TAG, "Size: " + iResult.contentStores.size() + "," + iResult.attrStores.size());
		updateData(iResult.contentStores);
		
		if(iResult.attrStores.size() > 0)
		{
			lastTimestame = iResult.attrStores.get(0)[iResult.attrStores.get(0).length-1];
			TDStockLog.d(TAG, "lastTimestame:" + lastTimestame);
		}
	}

	private void updateData(List<String[]> contentStores)
	{
		try
		{
			if(lastTimestame != null)
			{
				_list.remove(_list.size() - 1);
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		if(contentStores.size() > 0)
		{
			String[] arr = contentStores.get(0);
			for (int i = 0; i < arr.length; i++)
			{
				if(arr[i] != null)
					_list.add(arr[i]);
			}
		}

		if (_list.size() <= 0)
		{
			String tmp = "-1";
			_list.add(tmp);
		}
		
		if(contentStores.size() > 0 && contentStores.get(0).length >= HttpAppInfo.PAGE_SIZE)
		{
			String tmp = "-2";
			_list.add(tmp);
		}
		else
		{
			String tmp = "-3";
			_list.add(tmp);
		}

		_adapter.notifyDataSetChanged();
	}

}
